﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.CodeDom.Compiler;

namespace Compiler.Compiler.CSharp
{
    public class CSharpClassCompiler : ICompiler
    {
        public bool Compile(string taskName, string code, string language, List<ParamInfo> paramInfos, List<string> references, List<string> usings, out List<string> message)
        {
            if (string.IsNullOrWhiteSpace(code))
                throw new ArgumentNullException();
            if (string.IsNullOrWhiteSpace(language))
                throw new ArgumentNullException();

            message = new List<string>();

            CompilerParameters parameters = new CompilerParameters();
            parameters.GenerateExecutable = false;
            if (references != null && references.Count > 0)
                references.ForEach(r => parameters.ReferencedAssemblies.Add(r));
            CodeDomProvider provider = CodeDomProvider.CreateProvider(language);
            CompilerResults result = provider.CompileAssemblyFromSource(parameters, code);
            if (result.Errors.Count > 0)
            {
                foreach (CompilerError error in result.Errors)
                    message.Add(error.ErrorText);
                return false;
            }
            else
                return true;

        }

    }
}
